<template>
  <fix-area ref="fixArea">
    <div style="width: 100%; display: flex; justify-content: space-between; align-items: flex-start">
      <form-block ref="form" :model="form">
        <el-form-item label="演员" prop="name">
          <h3 style="line-height: 40px;margin: 0;padding: 0">{{ actorDetails.name.trim().length > 0 ? actorDetails.name : '未配置姓名' }}</h3>
        </el-form-item>
      </form-block>
      <el-button type="primary" @click="createVisible = true">添加</el-button>
    </div>
    <div style="flex: 1; min-height: 0; width: 100%">
      <table-block :loading="loading" :data="schedulesList">
        <table-column label="排序" width="120px" prop="sort" fixed="left" />
        <table-column label="剧场名称" prop="name" fixed="left" />
        <table-column label="剧场地址" prop="address" />
        <table-column label="演出时间" prop="show_time" />
        <table-column label="排期状态" width="120px" prop="status" :use-slot="true">
          <template #default="{row}">
            <el-tag :type="row.status === 0 ? 'success' : 'danger'" size="small">{{ row.status === 0 ? '显示' : '隐藏' }}</el-tag>
          </template>
        </table-column>
        <table-column label="操作" width="220px" :use-slot="true">
          <template #default="{row}">
            <el-button type="warning" size="mini" @click="editSchedules(row.id)">编辑</el-button>
            <el-button type="danger" size="mini" @click="deleteSchedules(row.id)">删除</el-button>
          </template>
        </table-column>
      </table-block>
    </div>
    <pagination :current-page="form.page" :total="total" @current-change="currentChange" />
    <create v-if="createVisible" :id="id" :visible="createVisible" @cancel="createVisible = false" @enter="createEnter" />
    <edit v-if="editVisible" :id="id" :sid="sid" :visible="editVisible" @cancel="editVisible = false" @enter="editEnter" />
  </fix-area>
</template>
<script>
import { actorDetails, schedulesList, deleteSchedules } from '@/api/actor'
import Create from '@/views/actor/schedules/component/Create.vue'
import Edit from '@/views/actor/schedules/component/Edit.vue'

export default {
  name: 'Schedules',
  components: {
    Create, Edit
  },
  props: {
    id: {
      type: String,
      default: '-1'
    }
  },
  data() {
    return {
      editVisible: false,
      sid: -1,
      actorDetails: {
        name: ''
      },
      form: {
        performer_id: this.id,
        page: 1
      },
      schedulesList: [],
      total: 0,
      loading: false,
      createVisible: false
    }
  },
  async mounted() {
    try {
      this.loading = true
      const actorDetailsRes = await actorDetails({ id: this.id })
      this.actorDetails = actorDetailsRes.data
      try {
        this.loading = true
        const schedulesListRes = await schedulesList(this.form)
        this.schedulesList = schedulesListRes.data.data
        this.total = schedulesListRes.data.total
        this.loading = false
      } catch (error) {
        this.$errorMessage()
        this.loading = false
      }
    } catch (error) {
      this.$errorMessage()
      this.loading = false
    }
  },
  methods: {
    async currentChange(event) {
      try {
        this.loading = true
        this.form.page = event
        const schedulesListRes = await schedulesList(this.form)
        this.schedulesList = schedulesListRes.data.data
        this.total = schedulesListRes.data.total
        this.loading = false
      } catch (error) {
        this.$errorMessage()
        this.loading = false
      }
    },
    async deleteSchedules(sid) {
      try {
        await this.$warningConfirm('确认删除')
        try {
          this.loading = true
          await deleteSchedules({ sid })
          try {
            this.loading = true
            const schedulesListRes = await schedulesList(this.form)
            this.schedulesList = schedulesListRes.data.data
            this.total = schedulesListRes.data.total
            this.loading = false
          } catch (error) {
            this.$errorMessage()
            this.loading = false
            this.schedulesList = []
          }
        } catch (error) {
          this.$errorMessage()
          this.loading = true
        }
      } catch (error) {
        console.log('取消确认删除', error)
      }
    },
    editSchedules(sid) {
      this.sid = sid
      this.editVisible = true
    },
    async editEnter() {
      this.editVisible = false
      try {
        this.loading = true
        const schedulesListRes = await schedulesList(this.form)
        this.schedulesList = schedulesListRes.data.data
        this.total = schedulesListRes.data.total
        this.loading = false
      } catch (error) {
        this.$errorMessage()
        this.loading = false
      }
    },
    async createEnter() {
      this.createVisible = false
      try {
        this.loading = true
        const schedulesListRes = await schedulesList(this.form)
        this.schedulesList = schedulesListRes.data.data
        this.total = schedulesListRes.data.total
        this.loading = false
      } catch (error) {
        this.$errorMessage()
        this.loading = false
      }
    }
  }
}
</script>
